<template>
  <div>
    <el-form
      :inline="true"
      size="mini">
      <el-form-item :label="`已选数据下载 [ ${currentTableData.length} ]`">
        <el-button-group>
          <el-button
            type="primary"
            size="mini"
            :disabled="currentTableData.length === 0"
            @click="handleDownloadXlsx(currentTableData)">
            xlsx
          </el-button>
          <el-button
            type="primary"
            size="mini"
            :disabled="currentTableData.length === 0"
            @click="handleDownloadCsv(currentTableData)">
            csv
          </el-button>
        </el-button-group>
      </el-form-item>
      <el-form-item :label="`已选数据下载 [ ${multipleSelection.length} ]`">
        <el-button-group>
          <el-button
            type="primary"
            size="mini"
            :disabled="multipleSelection.length === 0"
            @click="handleDownloadXlsx(multipleSelection)">
            xlsx
          </el-button>
          <el-button
            type="primary"
            size="mini"
            :disabled="multipleSelection.length === 0"
            @click="handleDownloadCsv(multipleSelection)">
            csv
          </el-button>
        </el-button-group>
      </el-form-item>
    </el-form>

    <el-table
      :data="currentTableData"
      v-loading="loading"
      size="mini"
      stripe
      style="width: 100%;"
      @selection-change="handleSelectionChange">

      <el-table-column
        type="selection"
        width="55">
      </el-table-column>

      <el-table-column label="ID" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.id}}
        </template>
      </el-table-column>

      <el-table-column label="uuid" align="center" width="280">
        <template slot-scope="scope">
          <el-tag
            size="mini"
            type="success">
            {{scope.row.uuid}}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="项目名称" width="80" align="center">
        <template slot-scope="scope">
          {{scope.row.project_title}}
        </template>
      </el-table-column>

      <el-table-column label="项目创建者" width="80" align="center">
        <template slot-scope="scope">
          {{scope.row.project_creater}}
        </template>
      </el-table-column>

      <el-table-column label="项目更新人" width="80" align="center">
        <template slot-scope="scope">
          {{scope.row.project_updater}}
        </template>
      </el-table-column>

      <el-table-column label="项目url_id" width="150" align="center">
        <template slot-scope="scope">
          {{scope.row.link_id}}
        </template>
      </el-table-column>

      <el-table-column label="url链接" width="150" align="center">
        <template slot-scope="scope">
          {{scope.row.link_url}}
        </template>
      </el-table-column>

      <el-table-column label="更新时间" align="center">
        <template slot-scope="scope">
          {{dayjs(scope.row.update_at).format('YYYY-MM-DD HH:mm')}}
        </template>
      </el-table-column>

      <el-table-column label="创建时间" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{dayjs(scope.row.create_at).format('YYYY-MM-DD HH:mm')}}
        </template>
      </el-table-column>

      <el-table-column label="操作" :show-overflow-tooltip="true" width="160">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="edit(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>
  </div>
</template>

<script>
import dayjs from 'dayjs'
export default {
  name: 'PageMain',
  props: {
    tableData: {
      default: () => []
    },
    loading: {
      default: false
    }
  },
  data () {
    return {
      currentTableData: [],
      multipleSelection: [],
      downloadColumns: [
        { label: 'ID', prop: 'id' },
        { label: 'uuid', prop: 'uuid' },
        { label: '标题', prop: 'title' },
        { label: '内容', prop: 'content' },
        { label: '更新时间', prop: 'update_at' },
        { label: '创建时间', prop: 'create_at' }
      ]
    }
  },
  watch: {
    tableData: {
      handler (val) {
        this.currentTableData = val
      },
      immediate: true
    }
  },
  methods: {
    dayjs,
    del (row) {
      this.$emit('del', { id: row.id })
    },
    edit (row) {
      this.$emit('edit', row)
    },
    handleSwitchChange (val, index) {
      const oldValue = this.currentTableData[index]
      this.$set(this.currentTableData, index, {
        ...oldValue,
        type: val
      })
      // 注意 这里并没有把修改后的数据传递出去 如果需要的话请自行修改
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
      this.$emit('changeSel', val)
    },
    downloadDataTranslate (data) {
      return data.map(row => ({
        ...row,
        gender: row.gender === '1' ? '男' : '女'
      }))
    },
    handleDownloadXlsx (data) {
      this.$export.excel({
        title: '用户列表',
        columns: this.downloadColumns,
        data: this.downloadDataTranslate(data)
      })
        .then(() => {
          this.$message('导出表格成功')
        })
    },
    handleDownloadCsv (data) {
      this.$export.csv({
        title: '用户列表',
        columns: this.downloadColumns,
        data: this.downloadDataTranslate(data)
      })
        .then(() => {
          this.$message('导出CSV成功')
        })
    }
  }
}
</script>
